package controller;

import pojo.Record;
import pojo.dto.FloorData;
import service.RecordService;
import service.SeatService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

import pojo.account.Reader;

import java.util.List;

@WebServlet(name = "ReaderIndexController", value = "/ReaderIndexController")
public class ReaderIndexController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        Reader reader = (Reader) session.getAttribute("reader");
        String reserveMessage = null;
        List<FloorData> floorDataList = new SeatService().queryAllFloorData();
        RecordService service = new RecordService();
        Record recordInUse = service.getInUseById(reader.getId());
        Record recordUnUsed = service.getUnUsedById(reader.getId());

        if (recordInUse != null)
            reserveMessage = "您已预约座位：" + recordInUse.getFloor_name() + "---->>>>" + recordInUse.getNumber();
        else if (recordUnUsed != null)
            reserveMessage = "您已预约座位：" + recordUnUsed.getFloor_name() + "---->>>>" + recordUnUsed.getNumber();
        session.setAttribute("reserveMessage", reserveMessage);
        request.setAttribute("floorDataList", floorDataList);

        request.getRequestDispatcher("/view/reader/index.jsp").forward(request, response);
    }
}
